package com.hulk.dryad.web.controller;

import cn.ms.sequence.IdWorker;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.Limiter;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.CommonConstants;
import com.hulk.dryad.common.util.IDWorkerRadix;
import com.hulk.dryad.common.util.SecurityUtils;
import com.hulk.dryad.persistence.entity.SmsSendRecord;
import com.hulk.dryad.manage.provider.service.SmsSendRecordService;
import com.hulk.dryad.web.api.vo.SMSCheckReqVO;
import com.hulk.dryad.web.api.vo.SMSSendReqVO;
import com.hulk.dryad.web.oauth2.basic.service.WebUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * Created by liqingxue
 * <p/>
 * <p>2020/10/26
 */
@Api(value = " sms", tags = "消息模块")
@ApiSort(4)
@Slf4j
@RestController
@RequestMapping("/sms")
@RequiredArgsConstructor
public class SMSController {

    private final SmsSendRecordService smsSendRecordService;

    /**
     * <post>消息发送</post>
     *
     * @param req 消息发送
     * @return R
     */
    @Idempotent
    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 0)
    @ApiOperation(value = "send", notes = "消息发送")
    @PostMapping(value = "/send")
    public void send(@RequestBody @Valid SMSSendReqVO req) {
        String workId = IDWorkerRadix.getId();
        log.info("workId:[{}] 消息发送,入参:[{}]", workId, req);

		WebUser suer = SecurityUtils.getUser();
		// init
		SmsSendRecord record = new SmsSendRecord();
		record.setId(IdWorker.getId());
		record.setMobile(req.getReceiver());
		record.setSmsId(workId);
		record.setSmsType(req.getSmsType());
		record.setSmsCode(RandomStringUtils.randomNumeric(6));
		record.setVerifyFlag("N");
		record.setExpireTime(LocalDateTime.now().plusMinutes(5L));
		record.setReversion(1L);
		record.setCreatedTime(LocalDateTime.now());
		record.setCreatedBy(CommonConstants.DEFUAULT_CREATER);
		// record.insert();

    }

    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "check", notes = "验证码校验")
    @PostMapping(value = "/check")
    public R check(@RequestBody @Valid SMSCheckReqVO req) {
		String workId = IDWorkerRadix.getId();
        log.info("workId:[{}] 验证码校验,入参:[{}]", workId, req);

		WebUser suer = SecurityUtils.getUser();
		//do check
		// TODO: 2020/12/31
		//Thread.sleep(200000000000000000L);

		return R.ok();
    }



    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "check", notes = "验证码校验plus")
    @GetMapping(value = "/checkplus/{smsType}/{smsValue}")
    public R checkPlus(@PathVariable("smsType") String smsType, @PathVariable("smsValue") String smsValue) {
		String workId = IDWorkerRadix.getId();
        log.info("workId:[{}] 验证码校验,入参:smsType:[{}],smsValue:[{}]", workId, smsType, smsValue);
		WebUser suer = SecurityUtils.getUser();
		//do check
		// TODO: 2020/12/31
		//Thread.sleep(200000000000000000L);
        return R.ok();
    }
}
